package com.cryptape.cita_wallet.service;

import android.util.Log;

import com.cryptape.cita_wallet.service.http.TokenService;

import org.web3j.abi.datatypes.Type;

import java.util.List;
import java.util.concurrent.Callable;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by dengming on 6/2/20.
 */

public class MidQueryService {
    public static Observable<String> queryMid() {
        return TokenService.checkMemberExistsAddress()
                .subscribeOn(Schedulers.newThread())
                .flatMap(new Func1<Boolean, Observable<String>>() {
                    @Override
                    public Observable<String> call(Boolean aBoolean) {
                        return Observable.fromCallable(new Callable<String>() {
                            @Override
                            public String call() throws Exception {
                                Log.e("dym","MidQueryService  queryMember thread:"+Thread.currentThread());
                                List<Type> types = ContractService.queryMember();
                                if (types != null && types.size() > 2) {
                                    String midCode = types.get(1).getValue().toString();
                                    //                                    CytonApplication.getInstance().MIDCode = midCode;
                                    return midCode;
                                } else {
                                    return null;
                                    //                                    CytonApplication.getInstance().MIDCode = null;
//                                         return null;
                                }

                            }
                        });
                    }
                })
                .observeOn(AndroidSchedulers.mainThread());
        //                    @Override
//                    public Observable<String> call(Boolean aBoolean) {
//                        return Observable.fromCallable(new Callable<String>() {
//                            @Override
//                            public String call() throws Exception {
//                                List<Type> types = ContractService.queryMember();
//                                if (types != null && types.size() > 2) {
//                                    String midCode = types.get(1).getValue().toString();
////                                    CytonApplication.getInstance().MIDCode = midCode;
//                                    return midCode;
//                                } else {
////                                    CytonApplication.getInstance().MIDCode = null;
//                                    return null;
//                                }
//                            }
//                        });
//                    }

    }
}
